<template>
  <el-dialog
    :title="addForm.id ? '编辑部门' :'新增部门' "
    :visible="visible"
    width="50%"
    @close="onDiaClose"
  >
    <el-form
      ref="form"
      :rules="addFormRules"
      :model="addForm"
      label-width="80px"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="addForm.name" placeholder="长度在1 到50 个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="addForm.code" placeholder="长度在1 到50 个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="addForm.manager" placeholder="请选择" @focus="getDeptsManager">
          <el-option
            v-for="item in deptManager"
            :key="item.value"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="addForm.introduce" placeholder="长度在1 到50 个字符" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('input', false)">取 消</el-button>
      <el-button type="primary" @click="onSave">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getDepartment, addpartment, getpartment, editDepartment } from '@/api/department'
import { getEmployessListSimple } from '@/api/employess'
export default {
  model: {
    prop: 'visible'
  },
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    node: {
      type: Object,
      required: true
    }
  },
  data() {
    // 校验部门名称
    const validatorName = async(rule, value, callback) => {
      let flag = false
      if (this.addForm.id) {
        // 编辑
        // 获取所有数据  筛选出同级部门的数据并且不能等于自身
        const res = await getDepartment()

        const newArr = res.depts.filter(item => item.pid === this.node.pid && item.id === this.node.id)
        // 根据筛选出的数据去校验
        flag = newArr.some(item => item.name === value)
      } else {
        // 新增
        if (!this.node.children) return callback()
        flag = this.node.children.some(item => item.name === value)
      }
      if (flag) {
        callback(new Error('不允许部门名称重复'))
      } else {
        callback()
      }
    }

    // 校验部门编码
    const validatorCode = async(rule, value, callback) => {
      const res = await getDepartment()
      let flag = false
      if (this.addForm.id) {
        // 编辑校验规则
        const newArr = res.depts.filter(item => item.code !== this.node.code)
        flag = newArr.some(item => item.code === value)
      } else {
        // 新增校验规则
        flag = res.depts.some(item => item.code === value)
      }
      if (flag) return callback(new Error(value + '已存在，不允许部门编码重复'))
      callback()
    }
    return {
      addForm: {
        manager: '',
        name: '',
        introduce: '',
        code: ''
      },

      addFormRules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在1 到50 个字符', trigger: 'blur' },
          { validator: validatorName, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门负责人', trigger: 'blur' }

        ],
        introduce: [
          { required: true, message: '请输入部门部门介绍', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在1到50 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门部门编码', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' },
          { validator: validatorCode, trigger: 'blur' }

        ]
      },
      deptManager: []
    }
  },

  created() {},

  methods: {
    onDiaClose() {
      this.$emit('input', false)
      // 对表单重置
      this.$refs.form.resetFields()
      this.addForm = {
        manager: '',
        name: '',
        introduce: '',
        code: ''
      }
    },

    // 获取下拉时的部门负责人列表
    async  getDeptsManager() {
      const res = await getEmployessListSimple()
      this.deptManager = res
    },
    // 点击确认添加子部门的弹框
    async  onSave() {
      await this.$refs.form.validate()
      try {
        if (this.addForm.id) {
          await editDepartment(this.addForm)
          this.$message.success('修改成功')
          this.$emit('editSuccess')
        } else {
          // 先进行表单验证
          this.addForm.pid = this.node.id
          await addpartment(this.addForm)
          this.$message.success('添加成功')
          // 关弹框
          this.$emit('addSuccess')
        }
        this.onDiaClose()
      } catch (err) {
        console.log(err)
      }
    },

    async  getDeptInfo(id) {
      const res = await getpartment(id)
      this.addForm = res
    }
  }
}
</script>

<style scoped lang="less"></style>
